package commands;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import jesss.commands.JesssCommand;
import jesss.commands.JesssCommandFactoryImpl;
import jesss.options.JesssOptions;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.List;

import static org.mockito.Mockito.*;

/**
 * User: Lee
 * Date: 8/23/13
 * Time: 1:33 PM
 */
public class PutObjectCommandTests extends CommandTestsBase {

    private AmazonS3 mockS3 = mock(AmazonS3.class);
    private JesssOptions mockJesssOptions = mock(JesssOptions.class);

    @Before
    public void Setup() {
        stub(mockJesssOptions.getAmazonS3()).toReturn(mockS3);
        List<Bucket> testBuckets = makeTestBuckets(5);
        stub(mockS3.putObject(any(PutObjectRequest.class))).toReturn(new PutObjectResult());
        stub(mockJesssOptions.getCommandName()).toReturn("po");
        stub(mockJesssOptions.getShouldDisplayVerbose()).toReturn(true);
    }

    @Test
    public void CanPutObject() throws Exception {
        String[] args = {"-bn", "testBucket", "-k", "testPrefix", "-sf", "c:\\temp\\test.txt"};
        File tempFile = new File("c:\\temp\\test.txt");
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        JesssCommand putObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        putObjectCommand.executeCommand();
        verify(mockS3, times(1)).putObject(any(PutObjectRequest.class));

        if (tempFile.exists()) {
            tempFile.deleteOnExit();
        }
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingBucketParam() throws Exception {
        String[] args = {"-k", "testPrefix", "-sf", "c:\\temp\\test.txt"};
        JesssCommand putObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        putObjectCommand.executeCommand();
        verify(mockS3, times(1)).putObject(any(PutObjectRequest.class));
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingSourceFileParam() throws Exception {
        String[] args = {"-bn", "testBucket", "-k", "testPrefix"};
        JesssCommand putObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        putObjectCommand.executeCommand();
        verify(mockS3, times(1)).putObject(any(PutObjectRequest.class));
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingKeyParam() throws Exception {
        String[] args = {"-bn", "testBucket", "-sf", "c:\\temp\\test.txt"};
        JesssCommand putObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        putObjectCommand.executeCommand();
        verify(mockS3, times(1)).putObject(any(PutObjectRequest.class));
    }

}